import axios from 'axios'
import store from '../store/store'
import {Message} from 'view-design'

import $store from '../store/store'
import $router from '../router/router'

let loadingPageStatus;
const isTong = process.env.NODE_ENV === 'tong';
let ksiot = '/kakone';
if (isTong) {
    ksiot = '/'
}
const axiosInstance = axios.create({
    headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'language': window.localStorage.getItem('language') ? window.localStorage.getItem('language') : 'zh_CN'
    },
    baseURL: ksiot,
    timeout: 20000,
    withCredentials: false
});

// 自定义配置公共headers
axiosInstance.interceptors.request.use(
    config => {
        //全局LOADINNG ,通过参数loading = false ，可手动关闭当前LOADING
        const params = config.data || config.params
        config.headers.common['language'] = localStorage.getItem('language')
        // config.headers.common['baseUrl'] = window.location.host
        loadingPageStatus = (!params || (params && params.loading !== false))
        if (loadingPageStatus) {
            $store.dispatch('loadingPage', false)
        }
        if (store.state.token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.common['token'] = store.state.token
        }
        return config
    },
    err => {
        if (loadingPageStatus) {
            $store.dispatch('loadingPage', false);
        }

        return Promise.reject(err)
    });

// http response 拦截器
axiosInstance.interceptors.response.use(
    response => {
        if (loadingPageStatus) {
            $store.dispatch('loadingPage', false);
        }
        if (response.data) {
            let msg = response.data.message;
            switch (response.data.code) {
                case 2003:
                    Message.warning('身份认证过期');
                    localStorage.removeItem('ziyunFactoryPortalToken');
                    localStorage.removeItem('ziyunMenuFirstLevel');
                    localStorage.removeItem('ziyunMenuSecondLevel');
                    localStorage.removeItem('ziyunMenuthirdLevel');
                    localStorage.removeItem('ziyunPortalMenu');
                    setTimeout(() => {
                        $router.push({path: '/'})
                    }, 2000);
                    break;
                case 401:
                    Message.warning('身份认证过期');
                    localStorage.removeItem('ziyunFactoryPortalToken');
                    localStorage.removeItem('ziyunMenuFirstLevel');
                    localStorage.removeItem('ziyunMenuSecondLevel');
                    localStorage.removeItem('ziyunMenuthirdLevel');
                    localStorage.removeItem('ziyunPortalMenu');
                    setTimeout(() => {
                        $router.push({path: '/'})
                    }, 2000);
                    break;
                default:
                    break;
            }
        }
        return response.data
    },
    error => {
        if (loadingPageStatus) {
            $store.dispatch('loadingPage', false);
        }
        return Promise.reject(error)   // 返回接口返回的错误信息
    });

export default axiosInstance
